<?php
namespace bee;
/**
 * Klasa koja čuva stanje stranice u bazi
 *  @todo koristi se  još za održavanje stanja na $sys_page trebalo bi je lagano ukinuti
 *  @deprecated zamijeniti je lagano sa factory klasom
 */
class state extends object
{	/**
 * Enter description here...
 *
 * @var tmp $tmp
 */
	public $tmp;
	public $url;
	public $arr_val;
	public $id;
	public $type;
	//private $db;
	/**
	 * konstruktor klase
	 *
	 * @param int $type
	 * @param int $id
	 */
	function __construct($type = 1, $id = 0)
	{


// 	  $this->tmp = new tmp();
// 		$this->arr_val = array();
// 		$this->id = $id;
// 		$this->type = $type;
// 		$this->tmp->type = $type;

		if($id != 0)
		{	$this->load();//$id, $this);
		}
	}
	/**
	 * Učitava stanje stranice iz baze
	 *
	 * @return unknown
	 */
	function load()
	{	$this->tmp->SELECT($this->id);
		switch($this->type)
		{	case 1:
				$at = explode(';', $this->tmp->value);
				foreach ($at as $k => $v)
				{	if($v)
					{	$a = explode('=',$v);
						{	$this->arr_val[$a[0]] = $a[1];
						}
					}
				}
				return $this->arr_val;
				break;
			case 2:
				$this->url = $this->tmp->value;
				return $this->url;
			default:
		}
	}
	/**
	 * Sprema stanje stranice u bazu
	 *
	 * @param mixed $obj_arr
	 * @return int
	 */
	function save($obj_arr = '')
	{	if(is_array($obj_arr) || is_object($obj_arr))
		{	foreach ($obj_arr as $key => $value)
			{	$this->arr_val[$key] = "$value";//$this->arr_val[$key] = "$key=$value";
			}
		}	elseif($obj_arr)
		{	$this->url = $obj_arr;
		}
		switch($this->type)
		{	case 1:
				$a = array();
				foreach ($this->arr_val as $k => $v)
				{	$a[] = "$k=$v";
				}
				$this->tmp->value =  implode(';', $a);
				break;
			case 2:
				$this->tmp->value = $this->url;
				break;
			default:
		}
		@$this->tmp->value = $this->tmp->value ? $this->tmp->value : ';';
		@$this->tmp->INSERT();
		return $this->tmp->id;
	}
	/**
	 * Dohva�a svojstvo stanja
	 *
	 * @param mixed $index
	 * @return mixed
	 */
	function getField($index)
	{	if(array_key_exists($index, $this->arr_val))
		{	return $this->arr_val[$index];
		}	else
		{	return false;
		}
	}
	/**
	 * Postavlja svosjtvo stanja
	 *
	 * @param mixed $index
	 * @param mixed $value
	 */
	function setField($index, $value)
	{	$this->arr_val[$index] = "$value";
	}
	function removeField($index)
	{	unset($this->arr_val[$index]);
	}

	/**
	 * Postavlja / obnavlja stanje stranice
	 *
	 * @param array $arr
	 * @param bool $removeOld
	 */
	function setArray($arr, $removeOld = false, $Overwrite = true)
	{	if($removeOld)
		{	$this->arr_val = $arr;
		}	else
		{	foreach ($arr as $k => $v)
			{	if($Overwrite || !array_key_exists($k, $this->arr_val))
				$this->arr_val[$k] = $v;
			}
		}
	}
}
